import {
  USER_ADDRESS_SELCTDEFAULTADDRESS,
  EXPRESS_STANDARD_LIST,
  EXPRESS_TYPE_LIST,
  USER_Place_Order,
  TEST_PAY
} from "../../messages/uris";
import req from "../../utils/request";
import { toast } from "../../utils/toast";
// pages/adsfas.js
Page({
  mixins: [require("../../mixin/themeChanged")],
  /**
   * 页面的初始数据
   */
  data: {
    expressStandardNameArray: [],
    expressStandardList: ["请选择"],
    expressStandardIndex: 1,
    expressTypeNameArray: [],
    expressTypeList: ["请选择"],
    expressTypeIndex: 0,
    address: {},
    money: 0,
    expectTimeList: [
      "尽快送达",
    ],
    expectTimeIndex: 0, 
    //弹出层
    topTips: false,
    hide: false,
    tipValue: "",
  },

  /**
   * 更改规格
   * @param {Object} e
   */
  changeStandard: function (e) {
    this.setData({
      expressStandardIndex: e.detail.value,
    });

    // 计算金额
    this.setData({
      money: this.data.expressStandardList[e.detail.value].price,
    });
  },

  /**
   * 更改类型
   * @param {Object} e
   */
  changeType: function (e) {
    this.setData({
      expressTypeIndex: e.detail.value,
    });
  },
  /**
   * 期望送达时间
   * @param {Object} e
   */
  changeExpect: function (e) {
    this.setData({
      index: e.detail.value,
    });
  },

  /**
   * 发布订单
   */
  postOrder: function () {
    const that = this
    // console.log(`
    // ${JSON.stringify(this.data.address)}
    // ${JSON.stringify(this.data.expressStandardList[this.data.expressStandardIndex])},
    // ${JSON.stringify(this.data.expressTypeList[this.data.expressTypeIndex])},
    // ${JSON.stringify(this.data.courierCode)},
    // ${JSON.stringify(this.data.expectTimeList[this.data.expectTimeIndex])},
    // ${JSON.stringify(this.data.remark)},
    // `);
    
    // 请求后端接口
    // TODO 未设置 this.data.expectTimeList[this.data.expectTimeIndex]
    req.post(
      USER_Place_Order,
      {
        "addressId": this.data.address.addressId,
        "courierCode": this.data.courierCode,
        "expectDeliveryTime": null,
        "expressStandardId": this.data.expressStandardList[this.data.expressStandardIndex].expressStandardId,
        "expressTypeId": this.data.expressTypeList[this.data.expressTypeIndex].expressTypeId,
        "remark": this.data.remark
      },
      (res) => {
        if (res.data.result) {
          toast("下单成功")
        }
        // TODO 下单成功, 转跳去支付
        // req.post(TEST_PAY,{},(res)=>{},res=>{},res=>{},that)
        wx.switchTab({
          url: '../order_list/order_list',
        })
      },
      (res) => {},
      (res) => {
        wx.hideLoading();
      },that
    );
  },
  /**
   * 更新取件码
   * @param { Object } e
   */
  update_courierCode: function (e) {
    this.data.courierCode = e.detail.value;
  },
  /**
   * 更新备注
   * @param { Object } e
   */
  update_remark: function (e) {
    this.data.remark = e.detail.value;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    this.selectDefaultAddress();

    // 获取大小列表
    req.get(EXPRESS_STANDARD_LIST, {}, (res) => {
      let list = new Array();

      for (let index = 0; index < res.data.result.length; index++) {
        const element = res.data.result[index];
        list[index] = element.name;
      }
      that.setData({
        expressStandardList: res.data.result,
      });

      that.setData({
        expressStandardNameArray: list,
      });
          // 计算金额
    this.setData({
      money: this.data.expressStandardList[1].price,
    });
    },(res)=>{},(res)=>{},that);
    // 获取类型列表
    req.post(EXPRESS_TYPE_LIST, {}, (res) => {
      let list = new Array();

      for (let index = 0; index < res.data.result.length; index++) {
        const element = res.data.result[index];
        list[index] = element.typeName;
      }
      that.setData({
        expressTypeList: res.data.result,
      });
      that.setData({
        expressTypeNameArray: list,
      });
    },(res)=>{},(res)=>{},that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (option) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
    });
  },

  //进入用户选择的收货地址页面
  openAddressRight: function () {
    wx.navigateTo({
      url: "/pages/order/addressRight/addressmanage",
    });
  },

  //获取用户默认地址
  selectDefaultAddress: function () {
    const that = this;
    req.get(
      USER_ADDRESS_SELCTDEFAULTADDRESS,
      {},
      (resp) => {
        this.setData({
          address: resp.data.result,
        });
      },
      (resp) => {
        console.log("查询地址失败");
      },(res)=>{},that
    );
  },
});
